class MainController < ApplicationController
	
	FORM_FIELDS = [ :nome,  :email, :telefone, :cidade, :mensagem ]
	
	before_filter :find_errors, :only => [ :contato_telefonia, :contato_form ]
	
	def index
		@product = Product.find_random	
	end
	
	def empresa;  end
	
	def telefonia; end
	
	def cadastra_contato
		@contact = Contact.new( :email => params[:email] )
		if @contact.save
			render :action => 'contato_sucesso'
		else
			render :action => 'contato_falha'
		end
	end
	
	def contato_telefonia
		unless @errors.blank?
			render :action => 'telefonia' 
		else
			ContatoMailer.deliver_contato_de_telefonia( params )
			message_sent
		end
	end
	
	def contato_form
		unless @errors.blank?
			render :action => 'contato' 
		else
			ContatoMailer.deliver_contato( params )
			message_sent
		end		
	end
	
	def find_errors
		@errors = []
		FORM_FIELDS.each do |i|
			if params[i].blank?
				@errors << "O campo #{i.to_s.humanize} deve ser preenchido"
			end
		end
		
		if !params[:email].blank? and  !(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ =~params[:email])
			@errors << "O email informado (#{params[:email]}) não é válido"
		end
		
	end
	
	def contato; end
	
	def clientes; end
	
	def servicos; end
	
	def message_sent
		render :action => 'message_sent'
	end
	
end